﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AssessmentTool.PresentationLogic;
using System.Windows.Data;

namespace AssessmentTool
{
	public partial class CompetenzaContainer : UserControl, IContainer, IDisposable
	{
        private ModelLayer.Competenza competenza;
        private bool opened;
        private IContainer parent;
        
     
		public CompetenzaContainer()
		{
			// Required to initialize variables
			InitializeComponent();
            competenceProgess.Maximum = 0;
            opened = false;
		}
        public void initWithCompetenza(ModelLayer.Competenza competenza)
        {
            this.competenza = competenza;
            txtCodice.Text = competenza.Codice + ".";
            txtTitolo.Text = new ApplicationResources().Get(competenza.Codice);
            if (competenza.Livello == 1)
            {
                HeadeRow.Height = new GridLength(30);
                Barra.Height = 30;
                this.Margin = new Thickness(0, 0, 0, 10);
                txtCodice.FontSize = 22;
                txtTitolo.FontSize = 20;
                border.Margin = new Thickness(15, 0, 0, 0);
            }
            this.Name = competenza.Codice;
           
        }

        public void setIContainerParent(IContainer parent)
        {
            this.parent = parent;
        }

        public void addProgress()
        {
            competenceProgess.Value += 1;
            checkValue();
            if(parent!=null)
                parent.addProgress();
            
        }
        private void checkValue()
        {
            if (competenceProgess.Value == competenceProgess.Maximum)
            {   
                competenceProgess.Foreground=(Brush)(new ColorConverter().Convert("#FF00CC0C",null,null,null));  
            }
            else
            {
                competenceProgess.Foreground = (Brush)(new ColorConverter().Convert("#FFFF0000", null, null, null));  
            }
        }

        public void addMaximumProgress()
        {
            competenceProgess.Maximum += 1;
            checkValue();
            if (parent != null)
                parent.addMaximumProgress();
        }

        
        


        public void addChild(UserControl competenza)
        {
            IContainer container = (IContainer)competenza;
            container.setIContainerParent(this);
            competenza.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
            InnerContent.Children.Add(competenza);
           
        }

        private void Barra_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
           
            if (!opened)
            {
                OpenHeightFrame.Value = InnerContent.ActualHeight;
                Open.Completed += new EventHandler(Open_Completed);
                Open.Begin();
                
            }
            else {
                CloseHeightAnimation.From = InnerContent.ActualHeight;
                Close.Completed += new EventHandler(Close_Completed);
                Close.Begin();
            }
        }

        void Close_Completed(object sender, EventArgs e)
        {
            
            opened = false;
        }

        

        void Open_Completed(object sender, EventArgs e)
        {
            Binding binding = new Binding("Height");
            binding.Source = InnerContent;
            binding.Mode = BindingMode.OneWay;
            border.SetBinding(Border.HeightProperty, binding);
            opened = true;
            
        }


        public void Dispose()
        {
            InnerContent.Children.Clear();
        }





        public void enableRisposta(bool enable)
        {
            foreach (IContainer container in InnerContent.Children)
            {
                container.enableRisposta(enable);
            }
        }
    }
}